package yoyo.secret.app;

import java.util.List;

import yoyo.secret.app.db.DataBaseOperation;
import yoyo.secret.app.model.UserInfo;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class AccountInfoListFragment extends ListFragment {

	private static DataBaseOperation mDataBaseOperation;
	private static String classification;
	private List<UserInfo> userInfos;
	private DataAdapter adapter;

	static AccountInfoListFragment newInstance(DataBaseOperation operation, String classinfo) {
		AccountInfoListFragment fragment = new AccountInfoListFragment();
		mDataBaseOperation = operation;
		classification = classinfo;
		return fragment;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		getAccountList();
		adapter = new DataAdapter();
		setListAdapter(adapter);
	}

	public DataAdapter getAdapter() {
		getAccountList();
		return adapter;
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
	}

	private void getAccountList() {
		userInfos = mDataBaseOperation.getUserInfo(classification);
	}

	class DataAdapter extends BaseAdapter {

		public DataAdapter() {

		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return userInfos.size();
		}

		@Override
		public Object getItem(int location) {
			// TODO Auto-generated method stub
			return userInfos.get(location);
		}

		@Override
		public long getItemId(int location) {
			// TODO Auto-generated method stub
			return location;
		}

		@Override
		public View getView(int location, View convertView, ViewGroup parent) {
			UserInfo userInfo = userInfos.get(location);
			ViewHodler hodler;
			if (convertView == null) {
				hodler = new ViewHodler();
				convertView = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.account_list_item, null);
				hodler.textView1 = (TextView) convertView.findViewById(R.id.account_name);
				hodler.textView2 = (TextView) convertView.findViewById(R.id.account_info);
				convertView.setTag(hodler);
			} else {
				hodler = (ViewHodler) convertView.getTag();
			}

			hodler.textView1.setText(userInfo.name);
			hodler.textView2.setText(userInfo.account);

			return convertView;
		}

		class ViewHodler {
			public TextView textView1, textView2;
		}

	}

}
